import java.util.Scanner;
  
public class Main {
    public final static Integer PN =7;
    public final static Integer PAGESIZE =1024;
    public static void main(String[] args) {
        Pagetab[] s = new Pagetab[PN];
        for (int i = 0; i < PN; i++) {
            s[i] = new Pagetab();
            if(i < 3) {
                s[i].P = 1;
            } else {
                s[i].bno = 0;
                s[i].P = 0;
            }
        }
        s[0].bno = 9;
        s[1].bno = 5;
        s[2].bno = 4;
        while (true) {
            System.out.println("系统菜单");
            System.out.println("1: change");
            System.out.println("2: show");
            System.out.println("0: exit");
            System.out.println("-----------------------");
            System.out.println("请输入：");
            Scanner scanner = new Scanner(System.in);
            int sel = scanner.nextInt();
            switch (sel) {
                case 1:
                    change(s);
                    break;
                case 2:
                    show(s);
                    break;
                case 0:
                    return;
                default:
                    System.out.println("无效的选项！重新输入");
                    break;
            }
        }
    }
    public static void show(Pagetab[] s) {
        for (int i = 0; i < PN; i++) {
            System.out.println("页号：" + s[i].pno + "\n状态：" + s[i].P + "\n物理块号：" + s[i].bno);
        }
    }

    public static void change(Pagetab[] s) {
        int flag = 1;
        int x = 0;
        int weiyi;//位移量
        int place; // ，物理地址
        int found = 0;
        char st; // 缺页状态
        while (flag == 1) {
            st = 'N';
            System.out.print("请输入逻辑地址：");
            Scanner scanner = new Scanner(System.in);
            int a = scanner.nextInt();
            int p = a / PAGESIZE;//p为页号
            weiyi = a % PAGESIZE;
            if (p >= PN) {
                System.out.println("逻辑地址非法！");
            } else {
                int i = 0;
                for (i = 0; i < PN; i++) {
                    if (s[i].pno == p) { // 利用页号p找到对应页
                        break;
                    }
                }
                if (s[i].P == 0) { // 状态为0
                    st = 'Y'; // 说明缺页
                    for (x = 0; x < PN; x++) { // s页
                        if (s[x].P == 1) {
                            found = 1;
                            break;
                        }
                    } // 说明在s页中有状态为1的表，跳过下面的if
                    if (found == 0) { // 说明s页中没有状态为1的表，执行此if
                        for (x = 0; x < PN; x++) {
                            if (s[x].P == 1) {
                                break;
                            }
                        }
                    }
                    found = 0;
                    s[x].P = 0; // s页状态修改为0
                    s[i].bno = s[x].bno; // p页物理块号记为s页所在的物理块号
                    s[x].bno = 0; // s页块号修改为0
                    s[i].P = 1; // p页状态修改为1
                }
                place = s[i].bno * PAGESIZE + weiyi; // 物理地址=物理块号*页面大小+页内位移
                System.out.println("逻辑地址：" + a + "\n页号：" + p + "\n页内位移：" + weiyi + "\n是否缺页：" + st + "\n物理块：" + s[i].bno + "\n物理地址：" + place);
            }
            System.out.print("是否继续？(1:是 0:否):");
            flag = scanner.nextInt();
        }
    }

}